﻿using Books.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Books.SQLite
{
   public class ManagerFilesDownloadComplete
    {
       public static  bool SaveDataFilesDownloadComplete(FileDownLoadComplete data)
       {
           bool result = false;
           using (var db = new SQLiteConnection(App.DatabasePath))
           {
               string change = string.Empty;
               try
               {

                   var existingUser = (db.Table<FileDownLoadComplete>().Where(
                       c => c.BookId == data.BookId && c.FileName==data.FileName)).SingleOrDefault();

                   if (existingUser != null)
                   {
                       existingUser.BookId = data.BookId;
                       existingUser.FileName = data.FileName;
                       existingUser.IsDownLoadComplete = data.IsDownLoadComplete;
                       int success = db.Update(existingUser);
                   }
                   else
                   {
                       int success = db.Insert(new FileDownLoadComplete()
                       {
                          FileName=data.FileName,
                          IsDownLoadComplete=data.IsDownLoadComplete,
                          BookId=data.BookId
                       });
                   }
                   result = true;
               }
               catch (Exception ex)
               {
                   result = false;
               }
           }
           return result;
       }
       public static ObservableCollection<FileDownLoadComplete> GetDataDownLoadFiles()
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<FileDownLoadComplete>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                    return new ObservableCollection<FileDownLoadComplete>(dataUser);
                else
                    return null;
            }

        }
       public static FileDownLoadComplete CheckExitsFile(string fileName)
       {
           using (var db = new SQLiteConnection(App.DatabasePath))
           {
               var dataUser = (db.Table<FileDownLoadComplete>()).FirstOrDefault(m => m.FileName == fileName);
               if (dataUser != null)
                   return dataUser;
               else
                   return null;
           }

       }
       public static bool CheckExitsFolder(string folderId)
       {
           using (var db = new SQLiteConnection(App.DatabasePath))
           {
               var dataUser = (db.Table<FileDownLoadComplete>()).FirstOrDefault(m => m.BookId == folderId);
               if (dataUser != null)
                   return true;
               else
                   return false;
           }

       }
    }
}
